package io.shallwe._admin.basedata.hangerframe;

import com.google.common.collect.Maps;
import com.jfinal.aop.Aop;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import io.shallwe._admin.basedata.init.StaticMapService;
import io.shallwe.common.model.HangerFrameMiddle;
import io.shallwe.common.model.StaticHanger;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 挂框号、电泳框号绑定
 * @author : 王小伟
 * @date : 2019-07-18 14:46:53
 */
public class HangerFrameService {
    private HangerFrameMiddle hangerFrameMiddleDao = new HangerFrameMiddle().dao();
    private StaticMapService staticMapService= Aop.get(StaticMapService.class);


    /**
     * 列出所有挂具、电泳框号绑定信息
     * @return
     */
    public List<HangerFrameMiddle> listHangerFrameMiddle(){
        List<HangerFrameMiddle> hangerFrameMiddleList = CacheKit.get("listHangerFrameMiddle","listHangerFrameMiddle");
        if(hangerFrameMiddleList==null){
            hangerFrameMiddleList = hangerFrameMiddleDao.template("hangerFrame.listHangerFrameMiddle").find();
            CacheKit.put("listHangerFrameMiddle" , "listHangerFrameMiddle" , hangerFrameMiddleList);
        }
        return hangerFrameMiddleList;
    }

    public HangerFrameMiddle findById(Integer id) {
        HangerFrameMiddle ret=CacheKit.get("hangerFrame",id);
        if(ret==null){
            List<HangerFrameMiddle> hangerFrameMiddles = this.listHangerFrameMiddle();
            for (HangerFrameMiddle hangerFrameMiddle : hangerFrameMiddles) {
                if(id==hangerFrameMiddle.getId().intValue()){
                    ret = hangerFrameMiddle;
                    CacheKit.put("hangerFrame" , id , ret);
                    break;
                }
            }
            if(ret==null) {
                ret = hangerFrameMiddleDao.findById(id);
            }
        }
        return ret;
    }

    public int getIdByHangerNo(String hangerNo){
        HangerFrameMiddle hangerFrameMiddle = this.findByHangerNo(hangerNo);
        if(hangerFrameMiddle!=null){
            return hangerFrameMiddle.getId();
        }
        return 0;
    }



    /***
     * 根据挂具编号查询挂具信息
     * @param hangerNo
     * @return
     */
    public HangerFrameMiddle findByHangerNo(String hangerNo){
        HangerFrameMiddle ret=CacheKit.get("hangerFrame",hangerNo);
        if(ret==null){
            List<HangerFrameMiddle> hangerFrameMiddles = this.listHangerFrameMiddle();
            for (HangerFrameMiddle hangerFrameMiddle : hangerFrameMiddles) {
                if(StrKit.equals(hangerNo,hangerFrameMiddle.getHangerNo())){
                    ret = hangerFrameMiddle;
                    break;
                }
            }
            if(ret==null) {
                ret = hangerFrameMiddleDao.template("hangerFrame.findByHangerNo", hangerNo).findFirst();
            }
            CacheKit.put("hangerFrame" , hangerNo , ret);
        }
        return ret;
    }

    /**
     * 根据扫描的挂具编号列出所有挂框所在的工位编码对照表
     * @param hangerNo
     * @return
     */
    public Map<String,String> getHangerLocationMap(String hangerNo){
        Map<String,String> ret=Maps.newHashMap();
        HangerFrameMiddle hangerFrameMiddle = this.findByHangerNo(hangerNo);
        Integer indexNo = hangerFrameMiddle.getId();
        int[] hangerSortArray = staticMapService.listHangerSortByScannerCode(indexNo);
        int length = hangerSortArray.length;
        Map<Integer, StaticHanger> hangerSortMap = staticMapService.getHangerSortMap();

        for (int i =0;i<length;i++) {
            int sort=i+1;
            int id=hangerSortArray[i];
            HangerFrameMiddle middle = this.findById(id);
            if(hangerFrameMiddle!=null){
                StaticHanger staticHanger = hangerSortMap.get(sort);
                String locationCode = staticHanger.getLocationCode();
                ret.put(middle.getHangerNo(), locationCode);
            }
        }
        return ret;
    }


    public String getLastHangerNo(String currentHangerNo){
        HangerFrameMiddle hangerFrameMiddle = this.findByHangerNo(currentHangerNo);
        int indexNo = hangerFrameMiddle.getId();
        int lastIndexNo = staticMapService.getLastIndexNo(indexNo);
        HangerFrameMiddle middle = this.findById(lastIndexNo);
        return middle.getHangerNo();
    }

    public String getNextHangerNo(String currentHangerNo){
        HangerFrameMiddle hangerFrameMiddle = this.findByHangerNo(currentHangerNo);
        int indexNo = hangerFrameMiddle.getId();
        int nextIndexNo = staticMapService.getNextIndexNo(indexNo);
        HangerFrameMiddle middle = this.findById(nextIndexNo);
        return middle.getHangerNo();
    }

    public Ret paginate(Integer offset, Integer pageSize, String hangerNo, String frameNo) {
        int pageNumber=offset/pageSize+1;
        Kv condition =Kv.by("hangerNo", hangerNo).set("frameNo",frameNo);
        Page<Record> paginate = Db.template("hangerFrame.paginate",condition).paginate(pageNumber, pageSize);
        return Ret.ok("total", paginate.getTotalRow()).set("rows", paginate.getList());
    }

    public Ret save(HangerFrameMiddle middle) {
        if(isFrameNoExist(middle.getFrameNo(),0)){
            return Ret.fail("msg","电泳框编号已存在！");
        }
        if(isHangerNoExist(middle.getHangerNo(),0)){
            return Ret.fail("msg","挂框编号已存在！");
        }
        if(middle.setUpdateTime(new Date()).update()) {
            //清除对应的缓存
            CacheKit.remove("hangerFrame",middle.getHangerNo());
            return Ret.ok("msg", "操作成功！");
        }else{
            return Ret.ok("msg", "操作失败！");
        }

    }

    public Ret update(HangerFrameMiddle middle) {
        if(isFrameNoExist(middle.getFrameNo(),middle.getId())){
            return Ret.fail("msg","电泳框编号已存在！");
        }
        if(isHangerNoExist(middle.getHangerNo(),middle.getId())){
            return Ret.fail("msg","挂框编号已存在！");
        }

        if(middle.setUpdateTime(new Date()).update()) {
            //清除对应的缓存
            CacheKit.remove("hangerFrame",middle.getHangerNo());
            return Ret.ok("msg", "操作成功！");
        }else{
            return Ret.ok("msg", "操作失败！");
        }
    }

    public boolean isHangerNoExist(String rackNo , Integer id){
        String sql="select id from t_hanger_frame_middle where hanger_no = ? and id != ?";
        return  Db.queryInt(sql,rackNo,id)!=null;
    }

    public boolean isFrameNoExist(String frameNo ,Integer id){
        String sql="select id from t_hanger_frame_middle where frame_no = ? and id != ?";
        return  Db.queryInt(sql,frameNo,id)!=null;
    }

    public Ret delete(String id) {
        HangerFrameMiddle middle = hangerFrameMiddleDao.findById(id);
        String hangerNo = middle.getHangerNo();
        if( hangerFrameMiddleDao.deleteById(id)) {
            //清除对应的缓存
            CacheKit.remove("hangerFrame",hangerNo);
            return Ret.ok("msg", "操作成功！");
        }else{
            return Ret.ok("msg", "操作失败！");
        }
    }
}
